package de.szut.ita13.sommerfeld.gui_soundboard;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; 
import java.util.ArrayList;



import de.szut.ita13.sommerfeld.audio.AudioButton;

/**
 * 
 * @author Rene Sommerfeld
 *
 */

public class GUI extends Frame {

	private static final long serialVersionUID = 1L;
	public static final String TITLE = "EpicSoundboard v1.0 by Rene Sommerfeld";
	
	public static ArrayList<AudioButton> audioButtons;
	public static Button editAudioButton;
	public static Button removeAudioButton;
	public static boolean EDITABLE = false;
	public static boolean REMOVABLE = false;
	public static final String NO_FILE = "NICHT BELEGT";
	
	public static final int BUTTON_IN_A_ROW = 3;
	public static final int DISTANCE_BUTTON_TOP = 10;
	public static final int DISTANCE_BUTTON_SIDE = 5;
	public static final int DISTANCE_TOP = 100;
	public static final int DISTANCE_SIDE = 30;
	public static final int NUMBER_OF_BUTTONS_PER_SIDE = 30;
	
	public static final Font mainFont = new Font("Arial",Font.CENTER_BASELINE,30);
	public static final Font counterLabelFont = new Font("Comic Sans", Font.CENTER_BASELINE, 12);
	
	public Label label;
	public static Label time;
	public static final String NO_PLAYED = "0";
	
	public GUI() {
		setTitle(TITLE);
		setSize(new Dimension(660,800));
		setLocation(new Point(100,100));
		setLayout(null);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
		setResizable(false);
		
		label = new Label("ITA13 Soundboard");
		label.setFont(mainFont);
		label.setSize(new Dimension(275, 50));
		label.setLocation(new Point(getWidth() / 2 - label.getWidth() / 2, 25));
		label.setVisible(true);
		
		time = new Label("0 remaining");
		time.setFont(counterLabelFont);
		time.setSize(new Dimension(250,20));
		time.setLocation(new Point(10, getHeight() - time.getHeight() - 10));
		time.setVisible(true);
		this.add(time);
		
		editAudioButton = new Button("Edit");
		editAudioButton.setForeground(Color.RED);
		editAudioButton.setSize(new Dimension(50,25));
		editAudioButton.setLocation(new Point(this.getWidth() - editAudioButton.getWidth(), this.getHeight() - editAudioButton.getHeight()));
		editAudioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(AudioButton.hasAudioButtonWithAudioFile(audioButtons) && !REMOVABLE) {
					EDITABLE = (EDITABLE) ? false : true;
					if(EDITABLE) editAudioButton.setForeground(Color.GREEN);
					else editAudioButton.setForeground(Color.RED);
					for(int i = 0; i < audioButtons.size(); i++) {
						if(audioButtons.get(i).hasAudioFile()) {
							audioButtons.get(i).setEditable(EDITABLE);
						}
					}
				}
			}
		});
		this.add(editAudioButton);
		
		
		removeAudioButton = new Button("Remove");
		removeAudioButton.setForeground(Color.RED);
		removeAudioButton.setSize(new Dimension(50,25));
		removeAudioButton.setLocation(new Point(editAudioButton.getX() - removeAudioButton.getWidth(), this.getHeight() - removeAudioButton.getHeight()));
		removeAudioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!EDITABLE && AudioButton.hasAudioButtonWithAudioFile(audioButtons)) {
					REMOVABLE = (REMOVABLE) ? false : true;
					if(REMOVABLE) removeAudioButton.setForeground(Color.GREEN);
					else removeAudioButton.setForeground(Color.RED);
					for(int i = 0; i < audioButtons.size(); i++) {
						if(audioButtons.get(i).hasAudioFile()) {
							audioButtons.get(i).setRemovable(REMOVABLE);
						}
					}
				}
			}
		});
		this.add(removeAudioButton);
					
		audioButtons = new ArrayList<AudioButton>();
		for(int i = 0; i < NUMBER_OF_BUTTONS_PER_SIDE; i++) {
			AudioButton audioButton = new AudioButton(NO_FILE);
			audioButton.setSize(new Dimension(200,50));
			audioButton.setBackground(Color.RED);
			audioButton.setLocation(new Point(DISTANCE_SIDE + audioButton.getWidth() * (i % BUTTON_IN_A_ROW) + (DISTANCE_BUTTON_SIDE * (i % BUTTON_IN_A_ROW)), DISTANCE_TOP + audioButton.getHeight() * (i / BUTTON_IN_A_ROW) + (DISTANCE_BUTTON_TOP * (i / BUTTON_IN_A_ROW))));
			Label counterLabel = new Label(NO_PLAYED);
			counterLabel.setFont(counterLabelFont);
			counterLabel.setSize(new Dimension(audioButton.getWidth() , DISTANCE_BUTTON_TOP));
			counterLabel.setLocation(new Point(audioButton.getX(), audioButton.getY() - counterLabel.getHeight()));
			audioButton.setCounterLabel(counterLabel);
			audioButton.getCounterLabel().setVisible(true);
			audioButton.setVisible(true);
			audioButtons.add(audioButton);
		}
		
			
		for(AudioButton aButton : audioButtons) {
			this.add(aButton);
			this.add(aButton.getCounterLabel());
		}

		this.add(label);

	}

}
